﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace UI_Editor.Widgets
{
    public partial class SpriteNode : UserControl
    {
        public SpriteNode parent;                                   //当前控件的父控件
        private List<SpriteNode> childs = new List<SpriteNode>();   //子控件

        /// <summary>
        /// 为当前控件添加子控件
        /// </summary>
        public void addChild(SpriteNode child)
        {
            if(!childs.Contains(child))
            {
                childs.Add(child);
                child.parent = this;
            }
        }

        /// <summary>
        /// 将当前控件从父控件中移除
        /// </summary>
        public void remove()
        {
            if (this.parent != null)
            {
                //移除子控件
                foreach (SpriteNode child in childs.ToArray())
                {
                    child.remove();
                }
                //移除当前控件
                parent.childs.Remove(this);
            }
        }

        private bool clicked = false;
        private PointF selectStartPos = PointF.Empty;
        private PointF selectCurPos = PointF.Empty;

        private Image backgroundImage;
        [Bindable(true), Category("渲染"), DefaultValue(null), Description("显示图像")]
        public Image BackImage
        {
            get { return backgroundImage; }
            set 
            { 
                backgroundImage = value;
                Size tmp = this.Size - backgroundImage.Size;
                this.Location += new Size(tmp.Width / 2, tmp.Height / 2); 
                this.Size = backgroundImage.Size; 
                //this.Invalidate(); 
            }
        }

        private string type = "Sprite";
        [Bindable(true), Category("渲染"), DefaultValue(null), Description("控件类型")]
        public string Type
        {
            get { return type; }
            //set { type = value; }
        }

        private string _name = "sprite1";
        [Bindable(true), Category("渲染"), DefaultValue(null), Description("控件名称")]
        public string name
        {
            get { return _name; }
            set { _name = value;}
        }

        public SpriteNode()
        {
            InitializeComponent();

            this.MouseDown += new MouseEventHandler(this.SpriteMouseDown);
            this.MouseMove += new MouseEventHandler(this.SpriteMouseMove);
            this.MouseUp += new MouseEventHandler(this.SpriteMouseUp);

            this.Size = new Size(100, 100);
            this.Location = new Point(-50, -50);
        }

        //protected override void OnPaintBackground(PaintEventArgs e)
        //{
        //    //不进行背景的绘制
        //}

        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
        //        return cp;
        //    }
        //}

        ///// <summary>
        ///// 在场景中进行当前控件的绘制
        ///// </summary>
        //protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        //{
        //    Graphics g = e.Graphics;

        //    if (backgroundImage != null) g.DrawImage(this.backgroundImage, this.Location);
        //    foreach (SpriteNode child in childs) child.OnPaint(e);
        //}

        /// <summary>
        /// 在场景中进行当前控件的绘制
        /// </summary>
        public void Paint_this(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            if (backgroundImage != null) g.DrawImage(this.backgroundImage, this.Location);
            paintCursorRect(g);

            GraphicsState transState = g.Save();
            g.TranslateTransform(Location.X, Location.Y);       //坐标偏移
            foreach (SpriteNode child in childs) child.Paint_this(e);
            g.Restore(transState);
        }


        public bool isSelect = false;

        /// <summary>
        /// 获取当前SpriteNode的区域范围 
        /// </summary>
        public Rectangle getRect()
        {
            return new Rectangle(Location.X, Location.Y, this.Width, this.Height);
        }
        public RectangleF getRectF()
        {
            return new RectangleF(Location.X, Location.Y, this.Width, this.Height);
        }

        /// <summary>
        /// 获取相对于渲染界面的坐标
        /// </summary>
        public RectangleF getScreenRectF()
        {
            PointF location = screenLoation();
            return new RectangleF(location.X, location.Y, this.Width, this.Height);
        }

        /// <summary>
        /// 获取相对于米格渲染界面的坐标
        /// </summary>
        public PointF screenLoation()
        {
            PointF location = new PointF(this.Left, this.Top);
            if (this.parent != null)
            {
                PointF parentLocation = parent.screenLoation();
                location.X += parentLocation.X;
                location.Y += parentLocation.Y;
            }
            return location;
        }

        /// <summary>
        /// 获取坐标P相对于当前控件的坐标，P为this的父控件坐标
        /// </summary>
        public PointF toRelative(PointF P)
        {
            return new PointF(P.X - Location.X, P.Y - Location.Y);
        }

        public PointF RelativePoint = PointF.Empty;
        /// <summary>
        /// 获取鼠标点击处的子控件
        /// </summary>
        public SpriteNode getSelectSprite(PointF P)
        {
            unSelect(); //先取消选中

            SpriteNode tmp = null;

            if (getRectF().Contains(P))
            {
                tmp = this;                         //先选中当前控件
                PointF P2 = toRelative(P);          //获取相对于当前控件的坐标
                RelativePoint = P2;                 //记录坐标

                //foreach (SpriteNode child in childs)//判断选中子控件
                for (int i = childs.Count - 1; i >= 0; i--)
                {
                    //SpriteNode tmp2 = child.getSelectSprite(P2);
                    SpriteNode tmp2 = childs[i].getSelectSprite(P2);
                    if (tmp2 != null)
                    {
                        tmp = tmp2;
                        break;
                    }
                }
            }

            return tmp;
        }

        /// <summary>
        /// 取消当前Sprite和其子控件的选中
        /// </summary>
        public void unSelect()
        {
            if (this != null) this.isSelect = false;
            foreach (SpriteNode child in childs)
            {
                child.unSelect();
            }
        }


        /// <summary>
        /// 选中控件时，绘制控件的尺寸调整外边框
        /// </summary>
        protected void paintCursorRect(Graphics g)
        {
            if (!isSelect) return;
            RectangleF rect = new RectangleF(Location.X, Location.Y, this.Width, this.Height);
            Pen pen = new Pen(Color.Black, 0.1f);
            //
            //外边框
            //
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            g.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
            //
            //四个顶点
            //
            paintSizeRect(rect.X, rect.Y, g);
            paintSizeRect(rect.Right, rect.Y, g);
            paintSizeRect(rect.X, rect.Bottom, g);
            paintSizeRect(rect.Right, rect.Bottom, g);
            //
            //边中点
            //
            paintSizeRect(rect.X + rect.Width / 2, rect.Y, g);
            paintSizeRect(rect.X + rect.Width / 2, rect.Bottom, g);
            paintSizeRect(rect.X, rect.Y + rect.Height / 2, g);
            paintSizeRect(rect.Right, rect.Y + rect.Height / 2, g);
        }

        protected void paintSizeRect(float x, float y, Graphics g)
        {
            x -= 3; y -= 3;
            g.FillRectangle(new SolidBrush(Color.White), x, y, 6, 6);
            g.DrawRectangle(new Pen(Color.Black, 0.05f), x, y, 6, 6);
        }


        protected void SpriteMouseDown(object sender, MouseEventArgs e)
        {
            bool isClickControl = (Control.ModifierKeys & Keys.Control) == Keys.Control;
            bool isClickAlt = (Control.ModifierKeys & Keys.Alt) == Keys.Alt;
            if (e.Button == MouseButtons.Left)
            {
                selectStartPos = selectCurPos = new PointF(e.X, e.Y);
                clicked = true;
            }
        }


        protected void SpriteMouseMove(object sender, MouseEventArgs e)
        {
            selectCurPos = new PointF(e.X, e.Y);

            System.Threading.Thread.Sleep(30);   //减少cpu占用率

            if (clicked)
            {
                SizeF moveSize = new SizeF(selectCurPos.X - selectStartPos.X, selectCurPos.Y - selectStartPos.Y);
                selectStartPos = selectCurPos;
                this.Location += new Size((int)moveSize.Width, (int)moveSize.Height);

                //if (renderScene != null) renderScene.Invalidate();  
                //this.Invalidate();
                Refresh();
            }
        }

        protected void SpriteMouseUp(object sender, MouseEventArgs e)
        {
            clicked = false;
        }

    }
}
